package org.wheel.quartz.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.quartz.JobExecutionContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.wheel.quartz.service.QuartzService;

import javax.annotation.Resource;

/**
 * 任务运行拦截并记录日志
 */
@Aspect
@ConditionalOnProperty(name = "spring.quartz.properties.log2db", havingValue = "true")
public class LogAspect {

    private QuartzService quartzService;

    @Pointcut("execution(public * org.wheel.quartz.DynamicQuartzJobBean.executeInternal(..))")
    public void executeInternal() { }

    @After("executeInternal()")
    void execute(JoinPoint joinPoint) {
        quartzService.saveQuartzLog((JobExecutionContext) joinPoint.getArgs()[0]);
    }

    @Resource
    public void setQuartzService(QuartzService quartzService) {
        this.quartzService = quartzService;
    }
}
